package gridmapper;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.ResourceBundle;

/**
 *
 * @author estell
 */
public class AppUtils {
	private String defaultGridSpace = "*ano";

	public String getNewRow(int columnCount){
		StringBuffer buffer = new StringBuffer();
		for (int i=0; i<columnCount; i++){
			buffer.append(defaultGridSpace);
			if (i<columnCount-1){
				buffer.append(",");
			}
		}
		return buffer.toString();
	}
	
	public ArrayList<GridSpace[]> buildNewMap(int rowCount, int columnCount){
		ArrayList<GridSpace[]> retVal = new ArrayList<GridSpace[]>();
		for (int i=0; i<rowCount; i++){
			GridSpace[] grdAry = new GridSpace[columnCount];
			for (int j=0; j<grdAry.length; j++){
				GridSpace spc = new GridSpace();
				spc.setType(Constants.Type.TYPE_GROUND);
				spc.setSubtype("a");
				spc.setTileColor(Color.GREEN);
				spc.setPhase(Constants.Phase.OPEN);
				spc.setOrientation(Constants.Orientation.NORTH);
				spc.setXpos(i);
				spc.setYpos(j);

				grdAry[j] = spc;
			}
			retVal.add(grdAry);
		}
		return retVal;
	}

	public BufferedImage getTileImage(BufferedImage imageResource, ResourceBundle rb, Constants.Type type, String subtype){
		String tmpVal = "";
		tmpVal = rb.getString(type.name());

		int tileWid = Integer.parseInt(rb.getString("tileWidth"));
		int tileHeight = Integer.parseInt(rb.getString("tileHeight"));

		String[] args = tmpVal.split(",");
		String[] subArgs = args[1].split("-");
		int xpos = Integer.parseInt(subArgs[0]) * tileHeight;//60
		int ypos = Integer.parseInt(args[0]) * tileWid;

		BufferedImage rsltImg = makeSubimage(imageResource, xpos, ypos, tileWid, tileHeight);
		return rsltImg;
	}

    //====================================================
    /**
     * @param img
     * @param x
     * @param y
     * @param w
     * @param h
     * @param mult
     * @return
     */
    protected BufferedImage makeSubimage(BufferedImage img,
        int x,
        int y,
        int w,
        int h,
        int mult) {

        BufferedImage subImg = new BufferedImage(w * mult, h, BufferedImage.TYPE_INT_ARGB);
        for (int m = 0; m < mult; m++) {
            subImg.getGraphics().drawImage(img.getSubimage(x, y, w, h), m * w, 0, null);
        }
        return subImg;
    }



    // ====================================================
    /**
     * @param img
     * @param x
     * @param y
     * @param w
     * @param h
     * @return
     */
    protected BufferedImage makeSubimage(BufferedImage img,
        int x,
        int y,
        int w,
        int h) {


        BufferedImage subImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        subImg.getGraphics().drawImage(img.getSubimage(x, y, w, h), 0, 0, null);
        return subImg;
    }


}
